package com.lyc.campuslife.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lyc.campuslife.entity.ShopType;
import com.lyc.campuslife.mapper.ShopTypeMapper;
import com.lyc.campuslife.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.lyc.campuslife.utils.RedisConstants.CACHE_SHOPTYPE_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyc
 * @since 2024-05-07
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<ShopType> getList() {
        String key = CACHE_SHOPTYPE_KEY;
        String shopTypeList = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isNotBlank(shopTypeList)){
            List<ShopType> list = JSONUtil.toList(shopTypeList, ShopType.class);
            return  list;
        }
        List<ShopType> list = query().orderByAsc("sort").list();
        if(list==null){
            return null;
        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(list));
        return list;
    }
}
